

import javax.swing.*;

import java.awt.*;
import java.io.*;
import java.sql.*;
import java.util.*;

import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.JTextComponent;

import java.awt.event.*;

/**
 * A GUI JPanel for logging into the system
 */
public class GUI_LoginPanel extends JPanel
{
    private static User login_attempt_user    = null;
    private static int  login_attempt_strikes = 0;
    
    private JPanel           loginPanel;
    private JTextField       unField;
    private JPasswordField   pwField;
    private JButton          submitButton;
    private JLabel           helpInfoLabel0;
    private JLabel           helpInfoLabel1;
    private JEditorPane      helpInfoLabel2;

    /**
     * Creates a new instance of GUI_LoginPanel
     */
    public GUI_LoginPanel()
    {
        unField = new JTextField();
        pwField = new JPasswordField();
        submitButton = new JButton("Submit");

        loginPanel = new JPanel();
        loginPanel.setLayout(new GridBagLayout());
        loginPanel.add(new JLabel("Username: "), GUI_Main.constraints(0, 0, 1, 1, 0.2, 1.0));
        loginPanel.add(unField,                  GUI_Main.constraints(1, 0, 1, 1, 1.0, 1.0));
        loginPanel.add(new JLabel("Password: "), GUI_Main.constraints(0, 1, 1, 1, 0.2, 1.0));
        loginPanel.add(pwField,                  GUI_Main.constraints(1, 1, 1, 1, 1.0, 1.0));
        loginPanel.add(submitButton,             GUI_Main.constraints(0, 2, 2, 1, 1.0, 1.0));
        loginPanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED, Color.BLACK, Color.BLUE), "Login"));

        helpInfoLabel0 = new JLabel("-Enter your username and password to access the site\n");
        helpInfoLabel1 = new JLabel("-For further assistance, call the system admin at 555.555.2340 or email \"it.s@over.9000\"");
        helpInfoLabel2 = new JEditorPane("text/html", "<span style=\"font: '" + helpInfoLabel1.getFont().getFontName()
                                                    + "'; font-size: " + helpInfoLabel1.getFont().getSize()
                                                    + "pt; font-weight:bold\">Visit <a href=\"http://www.youtube.com/watch?v=oHg5SJYRHA0\">our website</a> for more information.</span>");
        helpInfoLabel2.setEditable(false);
        helpInfoLabel2.setOpaque(false);
        helpInfoLabel2.addHyperlinkListener(new ClickedHyperlinkListener());
        helpInfoLabel2.setFont(helpInfoLabel1.getFont());

        setLayout(new GridBagLayout());
        add(new JLabel(""), GUI_Main.constraints(0, 1, 3, 1, 1.0, 1.0));
        add(new JLabel(""), GUI_Main.constraints(0, 2, 1, 1, 1.0, 0.0));
        add(loginPanel,     GUI_Main.constraints(1, 2, 1, 1, 1.0, 0.4));
        add(new JLabel(""), GUI_Main.constraints(2, 2, 1, 1, 1.0, 0.0));
        add(new JLabel(""), GUI_Main.constraints(0, 3, 3, 1, 1.0, 1.0));
        add(helpInfoLabel0, GUI_Main.constraints(0, 4, 3, 1, 1.0, 0.0));
        add(helpInfoLabel1, GUI_Main.constraints(0, 5, 3, 1, 1.0, 0.0));
        add(helpInfoLabel2, GUI_Main.constraints(0, 6, 3, 1, 1.0, 0.0));
        add(new JLabel(""), GUI_Main.constraints(0, 7, 3, 1, 1.0, 1.0));
    }

    /**
     * Add an ActionListener to the submit button
     * @param a the ActionListener
     */
    public void addSubmitActionListener(ActionListener a)
    {
        submitButton.addActionListener(a);
    }

    /**
     * Clears the fields
     */
    public void clearFields()
    {
        unField.setText("");
        pwField.setText("");
    }
    
    /**
     * An ActionListener to attempt to login
     */
    private class SubmitButtonsListener implements ActionListener
    {
        public void actionPerformed(ActionEvent e)
        {
            
			JTextComponent loginPanel_unField = null;
			JPasswordField loginPanel_pwField = null;
			if ( Login.loginUser(loginPanel_unField.getText(), String.valueOf(loginPanel_pwField.getPassword())) )
            {
                // set respective user as logged in user
                login_attempt_strikes = 0;
                loginPanel_unField.setEnabled(false);
                loginPanel_pwField.setEnabled(false);
                loginPanel_pwField.setText("");
            }
            else
            {
                if ( login_attempt_user == Login.getUser(loginPanel_unField.getText()) )
                {
                    if ( ++login_attempt_strikes == 3 )
                    {
                        Login.changeUserSuspendState(login_attempt_user, false);
                    }
                }
                else
                {
                    login_attempt_strikes = 0;
                    login_attempt_user = Login.getUser(loginPanel_unField.getText());
                }
            }
        }
    }
    
    /**
     * A HyperlinkListener to open links in a JEditorPane.
     */
    private class ClickedHyperlinkListener implements HyperlinkListener
    {
        public void hyperlinkUpdate(HyperlinkEvent e)
        {
            if ( HyperlinkEvent.EventType.ACTIVATED.equals(e.getEventType()) )
            {
                if( !java.awt.Desktop.isDesktopSupported() )
                {
                    System.err.println( "Desktop is not supported (fatal)" );
                }
                else if ( !java.awt.Desktop.getDesktop().isSupported(java.awt.Desktop.Action.BROWSE))
                {
                    System.err.println( "Desktop doesn't support the browse action (fatal)" );
                }
                else
                {
                    try
                    {
                        java.net.URI uri = new java.net.URI(e.getURL().toString());
                        java.awt.Desktop.getDesktop().browse(uri);
                    }
                    catch ( Exception ex )
                    {
                        System.err.println( ex.getMessage() );
                    }
                }
            }
        }
    }
    
}
